/*
 * TabbedPaneDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "TabbedPaneDelegate.h"

#include "QIconAdapter.h"

// TabbedPaneDelegate :: Protected Constructor. --------------------------------

/*!
 * \brief Constructs new TabbedPaneDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj TabbedPane java object
 */
TabbedPaneDelegate::TabbedPaneDelegate(JNIEnv *env, jobject obj)
  : ContainerDelegate(env, obj, new QTabWidget())
{
  connectDirect(qTabWidget(), SIGNAL(currentChanged(int)),
                SLOT(currentChangedSlot(int)));
}

// TabbedPaneDelegate :: Native Calls. -----------------------------------------

/*!
 * \brief Inserts tab at specified position.
 *
 * \param env the JNIEnv pointer
 * \param tabDelegate the tab delegate
 * \param index the index
 * \param title the tab title
 */
void TabbedPaneDelegate::insertTab(JNIEnv *env, jlong tabDelegate, jint index,
                                   jstring title)
{
  Q_ASSERT(env);
  SWING_REENTRANT(insertTab, SWING_ENV_ARG, JLONG_ARG(tabDelegate),
                  JINT_ARG(index), JSTRING_ARG(title))
  qTabWidget()->insertTab(index, COMPONENT_CAST(tabDelegate)->qWidget(),
                          QStringAdapter(env, title));
  COMPONENT_CAST(tabDelegate)->setParentDelegate(this);
}

/*!
 * \brief Removes the specified child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 */
void TabbedPaneDelegate::removeChild(JNIEnv *env, jlong childDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(removeChild, SWING_ENV_ARG, JLONG_ARG(childDelegate))
  qTabWidget()->removeTab(
               qTabWidget()->indexOf(COMPONENT_CAST(childDelegate)->qWidget()));
  COMPONENT_CAST(childDelegate)->setParentDelegate(0);
}

/*!
 * \brief Sets tab placement
 *
 * \param env the JNIEnv pointer
 * \param tabPlacement the tab placement
 */
void TabbedPaneDelegate::setTabPlacement(JNIEnv *env, jint tabPlacement)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTabPlacement, SWING_ENV_ARG, JINT_ARG(tabPlacement))
  switch(tabPlacement) {
    case 1:
      qTabWidget()->setTabPosition(QTabWidget::North);
      break;
    case 2:
      qTabWidget()->setTabPosition(QTabWidget::West);
      break;
    case 3:
      qTabWidget()->setTabPosition(QTabWidget::South);
      break;
    case 4:
      qTabWidget()->setTabPosition(QTabWidget::East);
      break;
  }
}

/*!
 * \brief Enables/disables the specified tab.
 *
 * \param env the JNIEnv pointer
 * \param index the tab index
 * \param enabled \c true if enabled
 */
void TabbedPaneDelegate::setTabEnabled(JNIEnv *env, jint index,
                                       jboolean enabled)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTabEnabled, SWING_ENV_ARG, JINT_ARG(index),
                  JBOOLEAN_ARG(enabled))
  qTabWidget()->setTabEnabled(index, enabled);
}

/*!
 * \brief Sets tab title.
 *
 * \param env the JNIEnv pointer
 * \param index the tab index
 * \param titleText the title text
 */
void TabbedPaneDelegate::setTabTitleText(JNIEnv *env, jint index,
                                         jstring titleText)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTabTitleText, SWING_ENV_ARG, JINT_ARG(index),
                  JSTRING_ARG(titleText))
  qTabWidget()->setTabText(index, QStringAdapter(env, titleText));
}

/*!
 * \brief Sets tooltip of the specified tab.
 *
 * \param env the JNIEnv pointer
 * \param index the tab index
 * \param toolTipText the tool tip text
 */
void TabbedPaneDelegate::setTabToolTipText(JNIEnv *env, jint index,
                                           jstring toolTipText)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTabToolTipText, SWING_ENV_ARG, JINT_ARG(index),
                  JSTRING_ARG(toolTipText))
  qTabWidget()->setTabToolTip(index, QStringAdapter(env, toolTipText));
}

/*!
 * \brief Sets what's this text of the specified tab.
 *
 * \param env the JNIEnv pointer
 * \param index the tab index
 * \param whatsThisText the what's this text
 */
void TabbedPaneDelegate::setTabWhatsThisText(JNIEnv *env, jint index,
                                             jstring whatsThisText)
{
  Q_ASSERT(env);
  Q_ASSERT(whatsThisText);
  SWING_REENTRANT(setTabWhatsThisText, SWING_ENV_ARG, JINT_ARG(index),
                  JSTRING_ARG(whatsThisText))
  qTabWidget()->setTabWhatsThis(index, QStringAdapter(env, whatsThisText));
}

/*!
 * \brief Sets tab icon
 *
 * \param env the JNIEnv pointer
 * \param index the tab index
 * \param iconSet the tab icon
 */
void TabbedPaneDelegate::setTabIcon(JNIEnv *env, jint index, jobject iconSet)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTabIcon, SWING_ENV_ARG, JINT_ARG(index),
                  JOBJECT_ARG(iconSet))
  if (iconSet) {
    qTabWidget()->setTabIcon(index, QIconAdapter(env, iconSet));
  } else {
    qTabWidget()->setTabIcon(index, QIcon());
  }
}

/*!
 * \brief Sets currently selected tab index.
 *
 * \param env the JNIEnv pointer
 * \param index the index
 */
void TabbedPaneDelegate::setCurrentIndex(JNIEnv *env, jint index)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setCurrentIndex, SWING_ENV_ARG, JINT_ARG(index))
  qTabWidget()->setCurrentIndex(index);
}

// TabbedPaneDelegate :: Static Native Calls. ----------------------------------

/*!
 * \brief Initializes TabbedPaneDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the TabbedPane java class
 *
 * \exception FatalError if failed
 */
void TabbedPaneDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_currentChangedMID);
  if (!INIT_UMID(currentChanged, cls, "(I)V")) {
    FATAL_LOGGED_ERROR("TabbedPaneDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong TabbedPaneDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new TabbedPaneDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// TabbedPaneDelegate :: Native Callbacks. -------------------------------------

/*!
 * \brief Calls \c currentChanged method of the tabbed pan object.
 *
 * \param selectedIndex the selected index
 */
void TabbedPaneDelegate::currentChanged(jint selectedIndex)
{
  jniObject()->callVoidMethod(sm_currentChangedMID, selectedIndex);
}

// TabbedPaneDelegate :: Protected Method. -------------------------------------

/*!
 * \brief currentChanged slot.
 *
 * \param index the index
 */
void TabbedPaneDelegate::currentChangedSlot(int index)
{
  currentChanged(index);
}

// TabbedPaneDelegate :: Private Static Data Member. ---------------------------

jmethodID TabbedPaneDelegate::sm_currentChangedMID = 0;
